<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('menus', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('parent_id')->nullable()->comment('父菜单ID');
            $table->string('name', 100)->comment('菜单名称');
            $table->string('code', 50)->unique()->comment('菜单编码');
            $table->string('icon', 50)->nullable()->comment('菜单图标');
            $table->string('path', 200)->nullable()->comment('菜单路径');
            $table->string('component', 100)->nullable()->comment('前端组件');
            $table->enum('type', ['menu', 'button', 'page'])->default('menu')->comment('菜单类型');
            $table->text('description')->nullable()->comment('菜单描述');
            $table->json('permissions')->nullable()->comment('所需权限');
            $table->enum('status', ['active', 'inactive'])->default('active')->comment('菜单状态');
            $table->boolean('is_external')->default(false)->comment('是否外部链接');
            $table->boolean('is_visible')->default(true)->comment('是否可见');
            $table->integer('sort_order')->default(0)->comment('排序权重');
            $table->timestamps();
            
            // 外键约束
            $table->foreign('parent_id')->references('id')->on('menus')->onDelete('cascade');
            
            // 索引
            $table->index(['parent_id', 'status']);
            $table->index(['code', 'status']);
            $table->index(['type', 'status']);
            $table->index('sort_order');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('menus');
    }
};
